package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ArticleTable011MappingMapper;
import org.rcloud.io.mapper.ProductMapper;
import org.rcloud.io.mapper.ProductSiteMapper;
import org.rcloud.io.model.ArticleTable011Mapping;
import org.rcloud.io.model.Product;
import org.rcloud.io.model.ProductSite;
import org.rcloud.io.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/3/12.
 */
@Service(value="ProductService")
public class ProductServiceImpl implements ProductService{
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSiteMapper psm;

    @Autowired
    private ArticleTable011MappingMapper atmm;

    @Override
    public PageVo<Product> findProduct() {
        try {
            List<Product> list = productMapper.selectAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findByEid(String eid) {

        try {
            Product product = productMapper.selectByEid(eid);
            return ResultVo.setOK(product);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo saveproduct(Product product) {

        if (product.equals("") || product == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                productMapper.insertSelective(product);
                return ResultVo.setOK(null);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

    }

    @Override
    public ResultVo findById(int id) {

        try {
            Product product = productMapper.selectByPrimaryKey(id);
            return ResultVo.setOK(product);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo updateProduct(Product product) {

        try {
            productMapper.updateByPrimaryKeySelective(product);
            return ResultVo.setOK(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }

    }

    @Override
    public ResultVo insertProductSite(ProductSite productSite) {
        if (productSite.equals("") || productSite == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = psm.insert(productSite);
                return ResultVo.setOK(i);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo updateProductSite(ProductSite productSite) {
        if (productSite.equals("") || productSite == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            try {
                int i = psm.updateByPrimaryKey(productSite);
                if (i == 1) {
                    return ResultVo.setOK(i);
                } else {
                    return ResultVo.setError("修改失败");
                }

            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public PageVo<ProductSite> findAllProductSite() {
        try {
            List<ProductSite> list = psm.findAll();
            return PageVo.setOk(list);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findProductSiteByEid(String eid) {
        try {
            ProductSite productSite = psm.findByEid(eid);
            return ResultVo.setOK(productSite);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo findProductSiteById(Integer id) {
        try {
            ProductSite productSite = psm.selectByPrimaryKey(id);
            return ResultVo.setOK(productSite);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.setError(e.getMessage());
        }
    }

    @Override
    public ResultVo savaMapping(ArticleTable011Mapping articleTable010Mapping) {

        if (articleTable010Mapping.equals("") || articleTable010Mapping == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            Integer aId = articleTable010Mapping.getaId();
            ArticleTable011Mapping byAId = null;
            try {
                byAId = atmm.findByAId(aId);

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (byAId != null) {
                    articleTable010Mapping.setId(byAId.getId());
                    atmm.updateByPrimaryKey(articleTable010Mapping);
                    return ResultVo.setOK("修改成功");
                } else {
                    atmm.insert(articleTable010Mapping);
                    return ResultVo.setOK("添加成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }
}
